package com.husd.framework.compile;

/**
 * @author shengdonghu@126.com
 * @date 2022/8/23 14:07
 */
import com.sun.source.tree.*;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.tree.JCTree;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SourceVisitorFeign extends TreeScanner<String, String> {

    Map<String,String> classAnnotationMap = new HashMap<>();
    public String jobHandler;

    public String getFeignClientValue() {

        return classAnnotationMap.getOrDefault("value","");
    }

    public String getFeignClientContextId() {

        return classAnnotationMap.getOrDefault("contextId","");
    }

    public String getFeignClientPath() {

        return classAnnotationMap.getOrDefault("path","");
    }

    @Override
    public String visitImport(ImportTree importTree, String s) {
        return super.visitImport(importTree, s);
    }

    @Override
    public String visitClass(ClassTree classTree, String s) {

        List<? extends AnnotationTree> annotationTreeList = classTree.getModifiers().getAnnotations();
        for(AnnotationTree annotationTree : annotationTreeList) {
            annotationTree.getAnnotationType();
            String name = annotationTree.getAnnotationType().toString();
            if(name.equals("FeignClient")) {
                //解析出来
                getAnnotationMap(annotationTree);
            } else if(name.equals("JobHandler")) {
                //
                this.jobHandler = getJobHandleValue(annotationTree);
            }
        }
        return super.visitClass(classTree, s);
    }

    private String getJobHandleValue(AnnotationTree annotationTree) {

        List<? extends ExpressionTree> list = annotationTree.getArguments();
        for(ExpressionTree expressionTree : list) {

            String value = expressionTree.toString();
            if(StringUtils.isNoneBlank(value)) {
                return value;
            }
        }
        return "";
    }

    @Override
    public String visitMethod(MethodTree node, String p) {

        return super.visitMethod(node,p);
    }

    // 把注解都解析出来
    private void getAnnotationMap(AnnotationTree annotationTree) {


        List<? extends ExpressionTree> list = annotationTree.getArguments();
        for(ExpressionTree expressionTree : list) {

            JCTree.JCAssign assign =  (JCTree.JCAssign)expressionTree;
            String key = assign.lhs.toString();
            String value = assign.rhs.toString().replaceAll("\"","");
            classAnnotationMap.put(key,value);
        }

    }

    @Override
    public String visitAnnotatedType(AnnotatedTypeTree node, String p) {

        return super.visitAnnotatedType(node,p);
    }

    @Override
    public String visitVariable(VariableTree variableTree, String s) {
      //  System.out.println("Find Variable Definition");
      //  System.out.println(variableTree.getType());
     //   System.out.println(variableTree.getName());
        return super.visitVariable(variableTree, s);
    }
}